package com.company.project.temp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Hello world!
 *
 */
public class AppMachineAppStatus {

	private static Map<String, Integer> map = new HashMap();

	public static void main(String[] args) {
//		 test();
//		test1();
		test2();


	}

	private static Long sum = 0l;



	private static void click() {

		MongoClient mongo = new MongoClient("pre_test.72solo.com", 27017);
		MongoDatabase db = mongo.getDatabase("inno72-prod-12021054");
		MongoCollection<Document> collection = db.getCollection("Inno72MachineInformation");
		BasicDBObject query = new BasicDBObject();




		// db.getCollection('Inno72MachineInformation').find({"type":"100100","activityId":"8ad75f62bca249fdac5e7b14680dde7d","machineCode":"18972441","sellerId":"519286239","serviceTime":{$gt:'2018-11-07 00:00:00 000',$lt:'2018-11-08 00:00:00 000'}}).sort({'serviceTime':-1})

		// query.put("machineCode", code);
//		query.put("type", "1");
//		query.put("pointTime", new BasicDBObject("$gte", "2018-10-01 00:00:00").append("$lte", "2018-10-07 23:59:59"));

		// todo excel
		collection.find(query).forEach(new Block<Document>() {

			@Override
			public void apply(Document next) {


			}

		});




		mongo.close();

	}

	private static BasicDBObject getLikeStr(String findStr) {
		Pattern pattern = Pattern.compile("^.*" + findStr + ".*$", Pattern.CASE_INSENSITIVE);
		return new BasicDBObject("$regex", pattern);
	}


	private static void test2() {


		MongoClient mongo = new MongoClient("pre_test.72solo.com", 27017);
		MongoDatabase db = mongo.getDatabase("inno72_prod_12031424");
		MongoCollection<Document> collection = db.getCollection("MachineAppStatus");
		BasicDBObject query = new BasicDBObject();
		// query.put("tag", getLikeStr("\"sdFree\":0"));
		// FileWriter fw = null;
		// fw = new FileWriter("/Users/72cy-0101-01-0009/Documents/test.txt");

		Set<String> machineIds = new HashSet<>();

		// FileWriter finalFw = fw;
		collection.find(query).forEach(new Block<Document>() {

			@Override
			public void apply(Document next) {
				String machineId = (String) next.get("machineId");
				List list = (List) next.get("status");
				for (Object o : list) {
					String appPackageName = (String)((Document) o).get("appPackageName");
					String versionName = (String)((Document) o).get("versionName");
					Integer versionCode = (Integer)((Document) o).get("versionCode");
					String appName = (String)((Document) o).get("appName");
					Integer appStatus = (Integer)((Document) o).get("appStatus");
					if (appPackageName.equals("com.inno72.installer") && versionName.equals("1.0.5")) {
						System.out.print("'" + machineId + "',");
					}
				}
			}

		});

		mongo.close();


	}

	private static void test1() {

		FileWriter fw = null;
		try {
			fw = new FileWriter("/Users/72cy-0101-01-0009/Documents/test.txt");
			String s = "hello world\n";
			fw.write(s, 0, s.length());
			fw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}


	static void count(String code, String startTime, String endTime) {

		MongoClient mongo = new MongoClient("pre_test.72solo.com", 27017);
		MongoDatabase db = mongo.getDatabase("inno72-prod-20181016");
		MongoCollection<Document> collection = db.getCollection("PointLog");
		BasicDBObject query = new BasicDBObject();
		query.put("machineCode", code);
		query.put("type", "1");
		query.put("pointTime", new BasicDBObject("$gte", "2018-10-01 00:00:00").append("$lte", "2018-10-07 23:59:59"));

		collection.find(query).forEach(new Block<Document>() {

			@Override
			public void apply(Document next) {

				String json = Optional.ofNullable(next.get("tag")).map(Object::toString).orElse("");
				if (StringUtils.isEmpty(json)) {
					json = Optional.ofNullable(next.get("detail")).map(Object::toString).orElse("");
				}

				String pointTime = Optional.ofNullable(next.get("pointTime")).map(Object::toString).orElse("");
				// System.out.println(pointTime);

				int s = compare_date(startTime, pointTime);
				int e = compare_date(pointTime, endTime);

				if (s == -1 && e == -1) {
					if (StringUtils.isNotEmpty(json)) {
						JSONObject jsonObject = JSON.parseObject(json);
						String count = Optional.ofNullable(jsonObject.get("count")).map(Object::toString).orElse("");
						if (StringUtils.isNotEmpty(count) && Integer.valueOf(count) != 0) {
							Integer mcount = map.get(code);
							System.out.println("mcount " + mcount);
							if (mcount != null) {
								map.put(code, mcount + Integer.valueOf(count));
							} else {
								map.put(code, Integer.valueOf(count));
							}
							// sum += Long.parseLong(count);
						}
					}
				}
			}

		});
		mongo.close();

		System.out.println(code);
	}

	public static int compare_date(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				// System.out.println("dt1 在dt2后");
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				// System.out.println("dt1在dt2前");
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
}
